<?php
//Check logged
function checkLogged($denypath = ""){
    checkloged();
    $denypath = $denypath ? $denypath : '../../resources/php/deny.php';
    //Check login
    $username = getValue('userlogin','str','SESSION','');
    $password = getValue('password','str','SESSION','');
    $admin_id 				= getValue("user_id","int","SESSION");
    $isAdmin	=	getValue("isAdmin", "int", "SESSION", 0);
    $db_check	= new db_query("SELECT adm_id 
								 FROM admin_users
								 WHERE adm_loginname = '" . $username . "' AND adm_password = '" . $password."'");
	if(mysql_num_rows($db_check->result) > 0){
		$check	= mysql_fetch_array($db_check->result);
		$adm_id	= $check["adm_id"];
		$db_check->close();
		unset($db_check);
		if($adm_id != $admin_id){
            redirect($denypath);
		}
	}else{
	   redirect($denypath);
	}
}

function checkLogin($username, $password){
	$username	= replaceMQ($username);
	$password	= replaceMQ($password);
	$db_check	= new db_query("SELECT adm_id 
							 FROM admin_users
							 WHERE adm_loginname = '" . $username . "' AND adm_password = '" . md5($password)."'");
	if(mysql_num_rows($db_check->result) > 0){
		$check	= mysql_fetch_array($db_check->result);
		$adm_id	= $check["adm_id"];
		$db_check->close();
		unset($db_check);
		return $adm_id;
	}
	else{
		$db_check->close();
		unset($db_check);
		return 0;
	}
}

//check access module
function checkAccessModule($module_id){
    $isAdmin	=	getValue("isAdmin", "int", "SESSION", 0);
    if($isAdmin){
        return true;
    }
    $db = new db_query('SELECT adu_admin_id FROM admin_users_right WHERE adu_admin_module_id = '.$module_id);
    if(mysql_num_rows($db->result)){
        unset($db);
        return true;
    }else{
        redirect('../../resources/php/deny.php');
    }
}

//Check loged
function checkloged(){
	$dm						= $_SERVER["SERVER_NAME"];
	$dm						= str_replace("www.", "", $dm);
	$db_select 				= new db_query("SELECT * FROM kdims WHERE kdm_domain = '" . md5($dm) . "' LIMIT 1");
	if($row = mysql_fetch_assoc($db_select->result)){
	       
			$array 					= str_debase($row["kdm_key"]);
			$row1 					= json_decode($array, true);
			if($row1 != null){
				if(md5($row["kdm_key"] . "|" . $row1["pass"]) != $row["kdm_hash"]){
					notifydie("Dang ky chua dung key");
				}else{
					return $row1;
				}
		}else{
			notifydie("Dang ky chua dung key");
		}
		
	}else{
		notifydie("Chua dang ky domain");
	}	
}
function checkPermission($action){
    global $module_id;
    $admin_id 				= getValue("user_id","int","SESSION");
    $isAdmin	=	getValue("isAdmin", "int", "SESSION", 0);
    if($isAdmin){
        return true;
    }

    $db = new db_query('SELECT adu_admin_'.$action.'
						FROM admin_users_right
						WHERE adu_admin_id = '.$admin_id.' AND adu_admin_module_id = '.$module_id);
    $result = mysql_fetch_assoc($db->result);
    if($result['adu_admin_'.$action] == 1){
        return true;
    }else{
        die('<div style="margin: 25px auto;padding: 10px;text-align: center"><h3>Bạn không có quyền truy cập chức năng này</h3></div>');
    }
}
function call_module_file($module_name,$action){
    if(file_exists('../'.$module_name.'/'.$action.'.php')){
        return '../'.$module_name.'/'.$action.'.php';
    }else{
        return '../../core/'.$module_name.'/'.$action.'.php';
    }
}
?>